/**
  ******************************************************************************
  * @file    uart_app.c
  * @author  Vincent
  * @version V1.0.0
  * @date    23-12-2015
  * @brief   
  ******************************************************************************
  *
  * COPYRIGHT(c) 2015 STMicroelectronics
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
  *   2. Redistributions in binary form must reproduce the above copyright notice,
  *      this list of conditions and the following disclaimer in the documentation
  *      and/or other materials provided with the distribution.
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  *      may be used to endorse or promote products derived from this software
  *      without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */
/* Includes ------------------------------------------------------------------*/
#include "uart_app.h"
#include "data_struct.h"
#include "string.h"
/* Private variables ---------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/
/******************************************************************************
* @Function:  void xuUser_Uart_Init(void)
* @Discrible:   串口初始化
* @Param:  
* @Return:   
* @Others: 
******************************************************************************
* @Recode    date       version	        author             modify 
*          ------------------------------------------------------------------
*          20161229      V1.0          VINCENT            Create
* 
*******************************************************************************/
void xuUser_Uart_Init(void)
{
//  MX_USART1_UART_Init();
//	__HAL_UART_ENABLE_IT(&huart1,UART_IT_RXNE);
	MX_USART2_UART_Init();
	__HAL_UART_ENABLE_IT(&huart2, UART_IT_RXNE);
	
}


/*******************************************************************************
* Function:  void UA_UartData_Process(USART_Buf_TypeDef * ptUARTRx)
* Discrible:
* Input:
* Output: 
* Return: 
* Others: 
*    date    version	  author        record
* -----------------------------------------------
* 206160402    V1.0     VINCENT       Create
*********************************************************************************/
void UA_UartData_Process(USART_Buf_TypeDef * ptUARTRx)
{
	uint8_t ucHeadHighByte,ucHeadLowByte,ucTailByte,ucDataLength,ucData,ucNum;
	uint8_t ucCheckSum=0,i;
	xuMsg_t ptMsg;

	while((*ptUARTRx).ucBufCnt > 5)
	{		
		ucHeadHighByte = UART_ReadChar(ptUARTRx,0);
		ucHeadLowByte = UART_ReadChar(ptUARTRx,1);

		if((0X55 == ucHeadHighByte)&&(0XAA == ucHeadLowByte))//判断帧头
		{			
		  ucDataLength = UART_ReadChar(ptUARTRx,2);
			if((*ptUARTRx).ucBufCnt >= (ucDataLength+3+2))
			{		
				for(ucNum=0;ucNum<(ucDataLength+2);ucNum++)//计算校验位
				{
					ucData = UART_ReadChar(ptUARTRx,2+ucNum);          				
					ucCheckSum ^= ucData;
				}
				ucTailByte=UART_ReadChar(ptUARTRx,2+ucNum);						
				if((0x00==ucCheckSum)&&(0xF0==ucTailByte))//判断校验位
				{
					UART_DelChar(ptUARTRx,3);
					while(ucDataLength > 0)    
					{
						ptMsg.usCmd= UART_ReadChar(ptUARTRx,0);  //读取有效数据长度
						ptMsg.usCmd <<= 8;
						ptMsg.usCmd+= UART_ReadChar(ptUARTRx,1);
						ptMsg.ucLength = ptMsg.usCmd&0x000F;

						if(ptMsg.ucLength <= ucDataLength)
						{
							UART_DelChar(ptUARTRx,2);
							for(i=0;i<ptMsg.ucLength;i++)
							{
									*(ptMsg.ucData+i) = UART_GetChar(ptUARTRx);
							}							
               				/* 使用消息队列实现指针变量的传递 */
							if( xQueueSend(xQueue1, &ptMsg,(TickType_t)10) == pdPASS )
							{
								/*队列发送成功，怎判断消息处理任务是否被挂起*/
							  if(eTaskGetState(osMsgProThreadHandle) == eSuspended)
								 {
									  /*如果被挂起则重新启动消息处理任务*/
									  osThreadResume(osMsgProThreadHandle);
								 }		
							}						
							ucDataLength -= (ptMsg.ucLength+2);
							if(ucDataLength < 2)
							{																	
								UART_DelChar(ptUARTRx,ucDataLength+2);
								return;
							}
							
						}  //数据长度和定义不对，删除正帧数据
						else
						{
							UART_DelChar(ptUARTRx,ucDataLength+2);
						}
					}	
				}
				else //校验位或者结束位不对，删掉帧头和长度
				{
					 UART_DelChar(ptUARTRx,1);
				}				
			}     
			else
			{
				/*数据未达到指定长度，则进入阻塞，等待300ms，*/
				osDelay(300);
				/*300m后解除阻塞，如果还未达到，则说明丢帧，删除首字节*/
				if((*ptUARTRx).ucBufCnt < (ucDataLength+3+2))
				{
					 UART_DelChar(ptUARTRx,1);
				}
			}
		}	
		else //帧头不对，删掉首字节
		{
			UART_DelChar(ptUARTRx, 1);
		}	 	   
	}
}



/*******************************************************************************
* Function: static void USER_UART_CmdTransimit(uint16_t ucCmd,uint8_t*pCmd,uint8_t ucLength)
* Discrible: 串口通信
* Input:
* Output: 
* Return: 
* Others: 
*    date    version	  author        record
* -----------------------------------------------
* 20160718     V1.0     VINCENT       Create
*********************************************************************************/
void User_UART_SendCmd(USART_Buf_TypeDef *uaUartID,uint16_t ucCmd,uint8_t*pCmd,uint8_t ucLength)
{	
	uint8_t uclength  = 0,ucXor = 0;
	uclength = ucLength+2;
	
	UART_PutChar(uaUartID,0x55);
	UART_PutChar(uaUartID,0xAA);
	UART_PutChar(uaUartID,uclength);
	ucXor ^= uclength;
	UART_PutChar(uaUartID,(ucCmd>>8));
	ucXor ^= (ucCmd>>8);
	UART_PutChar(uaUartID,ucCmd);
	ucXor ^= ucCmd;
	
	for(uint8_t i =0;i<ucLength;i++)
	{
		UART_PutChar(uaUartID,*(pCmd+i));
		ucXor ^= *(pCmd+i);
	}
	UART_PutChar(uaUartID,ucXor);
	UART_PutChar(uaUartID,0xF0);
}


/************************ (C) END OF FILE *********************************/




